package com.cntv.androidviewmmodel;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProvider;
import com.cntv.viewmodeldemo.databinding.ActivityAndroidVMBinding;
import com.cntv.viewmodeldemo.R;

/**
 * androidViewModel+livedata+lifcycle+databinding  使用ShareaPrefrence持久化储存
 */
public class AndroidVMActivity extends AppCompatActivity {
    MyAndroidViewModel viewModel;
    ActivityAndroidVMBinding dataBinding;//layout名字+binding

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_android_v_m);
//         viewModel= new ViewModelProvider(this)).get(MyAndroidViewModel.class);
         viewModel= new ViewModelProvider(this,new SavedStateViewModelFactory(getApplication(),this)).get(MyAndroidViewModel.class);
         dataBinding.setData(viewModel);
         dataBinding.setLifecycleOwner(this);
         getLifecycle().addObserver(viewModel);
    }

//    @Override
//    protected void onPause() {
//        super.onPause();
//        viewModel.save();
//    }
}